import os
import sys
import string


class HTMLPage:

	def __init__(self):
		self.title = ""
		self.form_action = ""
		self.form_name = ""
		self.form_method = "post"
		self.form_used = 0
		self.elem_info = []
		self.msg = []

	def addMessageCode(self,msg):
		self.msg.append(msg)	

	def addTitle(self,title):
		self.title = title

	def addForm(self,action,method,name):
		self.form_name = name
		self.form_action = action
		self.form_method = method
		self.form_used = 1
	
	def addElement(self,label,type,name,value):
		self.elem_info.append(label)
		tempStr = ""

		if len(type) > 0 and (type.upper() == "CHECKBOX" or type.upper() == "RADIO"):
			tempInfo = value.split(",")
			i = 0
			while i < len(tempInfo):
				tempStr = tempStr + "&nbsp;<input type='" + type + "' name='" + name + "' value='" + tempInfo[i] + "'>" + tempInfo[i]
				i = i + 1
		elif len(type) > 0:
			tempStr = "<input type='" + type + "' name='" + name + "'"
			if len(value) > 0:
				tempStr = tempStr + " value='" + value + "'"
			tempStr = tempStr +  ">"

		if len(tempStr) > 0:
			self.elem_info.append(tempStr)


	def printForm(self):
		print "<html><head><title>" + self.title + "</title></head><body>"
#		print "<div align='right'><a href='logout.html' style='color: blue; font-weight: bold;'>Log Out</a></div><br>"
		#Printing any messages added for form
		y = 0
		while y < len(self.msg):
			tempMsg = self.msg[y].split(":")
			print "<font style='color:" + str(tempMsg[1]) + "'>" + str(tempMsg[0]) + "</font>"
			y = y + 1
		
		if self.form_used == 1:
			print "<form action='" + self.form_action + "'"
			if len(self.form_name) > 0:
				print " name='" + self.form_name + "'"
			print " method='" + self.form_method + "'>"

		#Print body
		x = 0
		while x < len(self.elem_info):
			print self.elem_info[x]
			x = x + 1

		if self.form_used == 1:
			print "</form>"
		print "</body></html>"
		

	def printMainPage(self,uid):
		#Pulling up user ext from user_info.txt
		user_ext = ""
		try:
			f = open("user_info.txt","r")
			for line in f:
				tempInfo = line.strip()
				tempLine = tempInfo.split("|")
				if tempLine[0] == uid:
					user_ext = tempLine[3]
			f.close()
		except:
			print "Unable to open login information"

		#Checking user_info_cal.txt to see if any calendars were entered for this user
			
		print "<html><head><title>"
		print self.title
		print "</title></head><body>"
		print "<div align='right'><a href='logout.html' style='color: blue; font-weight: bold;'>Log Out</a></div>"
		print "<input type='hidden' name='uid' value='" + uid + "'>"
		print "<div align='center' style='font-weight: bold;font-size: 15 pt;'>Welcome " + uid+"<br>Your extention number is " + user_ext + "</div>"
		print "<div style='font-weight: bold;'>"
		print "<a href='add_calendar.cgi' style='color: blue;' align='center'>Add New Calendar</a>"
		print "</div>"
		print "<div style='font-weight: bold;'>Active Calendars</div>"
		try:
			#Printing calendar information for user
			g = open("user_info_cal.txt","r")
			line_fnd = 0
			for line in g:
				line_fnd = 1
				tempInfo = line.strip()
				tempLine = tempInfo.split("|")
				if tempLine[0] == uid:
					print "&nbsp;&nbsp;" + tempLine[1]
					print "<a href='" + tempLine[3] + "' style='color: blue;' target='_blank'> View</a><br>"

			if line_fnd == 0:
				print "No calendar information has been added"
			g.close()
		except:
			print "Sorry, an error has occured.  Unable to open user calendar file.<br>"

		print "<br><br><br>"
		print "<div style='font-weight: bold;'>"
		print "Weather Zipcodes<br><a href='add_zipcodes.cgi' style='color: blue;'>Add Zipcode</a>"
		try:
			w = open("user_info_zip.txt","r")
			line_fnd = 0
			for line in w:
				line_fnd = 1
				tempInfo = line.strip()
				tempLine = tempInfo.split("|")
				if tempLine[0] == uid:
					print "&nbsp;&nbsp;<br>" + tempLine[1]
					
			w.close()
			if line_fnd == 0:
				print "No zipcodes have been entered."
		except:
			print "Sorry, and error has occured.  Please try again later"

		print "</div>"
		print "</body></html>"




		
		
